package rest.business;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import model.AlarmUser;
import model.AlarmUserMapper;
import model.Grid;

import org.apache.ibatis.session.SqlSession;
import org.codehaus.jackson.map.ObjectMapper;

import utils.BeanUtils;
import core.BaseMapper;
import core.Pagination;
import core.TranscationCallback;
import core.TranscationTemplate;

@Path("/alarmuser")
public class AlarmUserResource {

	@SuppressWarnings("unchecked")
	@GET
	@Path("query")
	@Produces({ MediaType.APPLICATION_JSON })
	public List<AlarmUser> query()
			throws Exception {
		return (List<AlarmUser>) new TranscationTemplate().execute(new TranscationCallback() {

			@Override
			public Object doInSession(SqlSession session) {
				BaseMapper mapper = session.getMapper(AlarmUserMapper.class);
//				Pagination pagination = Pagination.newInstance(page);
				Map<String, Object> params = new HashMap<String, Object>();
				return mapper.query(params);
//				params.put("pagination", pagination);
//				grid.setRecords(mapper.query(params));
//				grid.setPagination(pagination);
//				Pagination pagination2 = Pagination
//						.newPaginationScope(pagination);
//				params = BeanUtils.clone(params);
//				params.put("pagination", pagination2);
//				grid.getPagination().setLastPage(mapper.count(params));
			}
		});
	}

	@SuppressWarnings("unchecked")
	@GET
	@Path("pagination")
	@Produces({ MediaType.APPLICATION_JSON })
	public Grid<AlarmUser> queryPagination(@QueryParam("rules") String rules)
			throws Exception {
		rules = URLDecoder.decode(rules, "UTF-8");
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> map = objectMapper.readValue(rules, Map.class);
		Object v = map.get("page");
		final int page;
		if (v instanceof Integer) {
			page = (Integer) v;
		} else {
			page = Integer.parseInt((String) v);
		}
		final Grid<AlarmUser> grid = new Grid<AlarmUser>();
		new TranscationTemplate().execute(new TranscationCallback() {

			@Override
			public Object doInSession(SqlSession session) {
				BaseMapper mapper = session.getMapper(AlarmUserMapper.class);
				Pagination pagination = Pagination.newInstance(page);
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("pagination", pagination);
				grid.setRecords(mapper.query(params));
				grid.setPagination(pagination);
				Pagination pagination2 = Pagination
						.newPaginationScope(pagination);
				params = BeanUtils.clone(params);
				params.put("pagination", pagination2);
				grid.getPagination().setLastPage(mapper.count(params));
				return null;
			}
		});
		return grid;
	}

	// @SuppressWarnings("unchecked")
	// @GET
	// @Path("query/{rules}")
	// @Produces({ MediaType.APPLICATION_JSON })
	// public List<AlarmUser> query2(@PathParam("rules") String rules)
	// throws Exception {
	// String alarmUserListJson = ResourceUtils.getWebapp(this)
	// + "/json/AlarmUserList.json";
	// ObjectMapper mapper = new ObjectMapper();
	// List<AlarmUser> alarmUserList = mapper.readValue(new File(
	// alarmUserListJson), List.class);
	// return alarmUserList;
	// }
}
